import "package:flutter/cupertino.dart";
import "package:flutter/material.dart" hide ExpandIcon;
import "package:flutter_easyrefresh/easy_refresh.dart";
import "package:flutter_screenutil/flutter_screenutil.dart";
import "package:hongsheng/app/SPClassApplicaion.dart";
import "package:hongsheng/model/SPClassBaseModelEntity.dart";
import "package:hongsheng/model/SPClassExpertListEntity.dart";
import "package:hongsheng/utils/SPClassCommonMethods.dart";
import "package:hongsheng/utils/SPClassImageUtil.dart";
import "package:hongsheng/utils/SPClassNavigatorUtils.dart";
import 'package:hongsheng/utils/SPClassStringUtils.dart';
import "package:hongsheng/api/SPClassApiManager.dart";
import "package:hongsheng/api/SPClassHttpCallBack.dart";
import "package:hongsheng/utils/SPClassToastUtils.dart";
import "package:hongsheng/pages/anylise/SPClassExpertDetailPage.dart";
import "package:hongsheng/pages/common/SPClassNoDataView.dart";
import "package:hongsheng/pages/home/SPClassHomePage.dart";
import "package:hongsheng/widgets/SPClassExpandIcon.dart";
import "package:hongsheng/widgets/SPClassPopupWindowButton.dart";
import "package:hongsheng/widgets/SPClassBallFooter.dart";
import "package:hongsheng/widgets/SPClassBallHeader.dart";
import 'package:hongsheng/model/SPClassTaskEntity.dart';
import 'package:hongsheng/widgets/SPClassTaskToast.dart';
// import 'SPClassLeaguePage.dart';

class SPClassExpertLeaderboardPage extends StatefulWidget{
  SPClassExpertLeaderboardPageState ?spProState;

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return spProState= SPClassExpertLeaderboardPageState();
  }

}

class SPClassExpertLeaderboardPageState extends State<SPClassExpertLeaderboardPage> with TickerProviderStateMixin ,AutomaticKeepAliveClientMixin{
  var spProTimeKey="近30天";
  var spProTimeKeys=[
    ["近期","近7天","近30天","全周期"],
    [ "近期","近7天","近30天","全周期"],
    ["近期","近7天","近30天","全周期"],
  ];

  var spProBoardKey="胜率榜";
  var spProBoardTitles=["胜率榜", "连红榜","回报率"];
  var spProBoardTopTitles=["胜率", "最高连红","回报率"];
  var spProOrderKeys=["correct_rate", "max_red_num","profit_sum"];
  int page=1;

  var spProShowBroadDown=false;
  var spProShowTimerDown=false;
  EasyRefreshController ?controller;
  List<SPClassExpertListExpertList> spProExpertList=[];
  String ?selectzqLeague='all';
  String ?selectlqLeague='all';

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    controller=EasyRefreshController();
    SPClassApplicaion.spProEventBus.on<String>().listen((event) {
      if(event.startsWith("scheme:refresh")){

        onRefresh();

      }
    });
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    super.build(context);
    return Container(
      child: Column(
        children: <Widget>[
          Container(
            width: MediaQuery.of(context).size.width,
            decoration: BoxDecoration(
              color: Colors.white,
              border: Border(
                  bottom: BorderSide(width: 0.4,color: Colors.grey[300]!)
              ),
            ),
            height: width(40),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                // GestureDetector(
                //   onTap: (){
                //     // SPClassNavigatorUtils.spFunPushRoute(context, SPClassLeaguePage(selectLeagueName: SPClassHomePageState.spProHomeMatchTypeKey=='is_zq_expert'?selectzqLeague:selectlqLeague,)).then((value){
                //     //   if(value==null)return;
                //     //   if(SPClassHomePageState.spProHomeMatchTypeKey=='is_zq_expert'){
                //     //     selectzqLeague = value;
                //     //   }else{
                //     //     selectlqLeague = value;
                //     //   }
                //     //   onRefresh();
                //     // });
                //   },
                //   child: Container(
                //     width: width(360)/3,
                //     height: width(40),
                //     child: Row(
                //       mainAxisAlignment: MainAxisAlignment.center,
                //       children: <Widget>[
                //         Text(SPClassStringUtils.spFunMaxLength(SPClassHomePageState.spProHomeMatchTypeKey=='is_zq_expert'?(selectzqLeague??'全部'):(selectlqLeague??'全部'),length: 7).replaceAll('all', '赛事筛选'),style: TextStyle(fontSize: sp(14),color: Color(0xFF333333)),),
                //         SPClassExpandIcon(
                //           padding: EdgeInsets.zero,
                //           isExpanded: false,
                //           color: Colors.grey[300]!,
                //         ),
                //       ],
                //     ),
                //   ),
                // ),
                SPClassPopupWindowButton(
                  child: Container(
                    width:width(360)/2,
                    height: width(40),
                    alignment: Alignment.center,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Image.asset(
                          SPClassImageUtil.spFunGetImagePath(spProBoardKey=='连红榜'?'ic_red_board':spProBoardKey=='胜率榜'?'ic_rate_board':'ic_return_board'),
                          width: width(57),
                        ),
                        SPClassExpandIcon(
                          padding: EdgeInsets.zero,
                          isExpanded: spProShowBroadDown,
                          color: Colors.grey[300]!,
                        ),
                      ],
                    ),
                  ) ,
                  offset: Offset(0,width(40),),
                  type: MaterialType.transparency,
                  windowChange: (value){
                    spProShowBroadDown=value;
                    setState(() {
                    });
                  },
                  window:Container(width:  MediaQuery.of(context).size.width,color: Colors.black.withOpacity(0.7),child:Column(
                    children: <Widget>[
                      Container(
                        color: Colors.white,
                        child: ListView(
                            padding: EdgeInsets.zero,
                            shrinkWrap: true,
                            physics: const NeverScrollableScrollPhysics(),
                            children: spProBoardTitles.map((item) => GestureDetector(
                              child:GestureDetector(
                                behavior: HitTestBehavior.opaque,
                                child: Container(
                                  decoration: BoxDecoration(
                                      color: Colors.white,
                                      border:Border(bottom: BorderSide(width: 0.4,color: Colors.grey[300]!))
                                  ),
                                  padding: EdgeInsets.symmetric(horizontal: width(15),vertical: width(8)),
                                  width: MediaQuery.of(context).size.width,
                                  child: Row(
                                    children: <Widget>[
                                      Text(item,style: TextStyle(color: spProBoardKey==item? Color(0xFFE3494B):Color(0xFF888888)),),
                                      Visibility(
                                        child: Expanded(
                                          child: Row(
                                            mainAxisAlignment: MainAxisAlignment.end,
                                            children: <Widget>[
                                              Icon(Icons.check_circle_outline,color: Color(0xFFE3494B),size: width(18),),
                                            ],
                                          ),
                                        ),
                                        visible: spProBoardKey==item ,
                                      )
                                    ],
                                  ),
                                ),
                                onTap: (){
                                  spProTimeKey="近期";


                                  spProExpertList.clear();
                                  setState(() {
                                    spProBoardKey=item;
                                  });
                                  controller?.callRefresh();
                                  Navigator.of(context).pop();
                                },
                              ) ,
                            )).toList()
                        ),
                      ),
                      Expanded(
                        child: GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          child: SizedBox(width:MediaQuery.of(context).size.width ,),
                          onTap: ()=>Navigator.of(context).pop(),
                        ),
                      )
                    ],
                  ),),
                ),
                // Container(width: 0.4,height: width(30),color: Colors.grey[300],),
                SPClassPopupWindowButton(
                  child: Container(
                    width: width(360)/2,
                    height: width(40),
                    alignment: Alignment.center,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Text(spProTimeKey,style: TextStyle(fontSize: sp(14)),),
                        SPClassExpandIcon(
                          padding: EdgeInsets.zero,
                          isExpanded: spProShowTimerDown,
                          color: Colors.grey[300],
                        ),
                      ],
                    ),
                  ) ,
                  offset: Offset(0,width(40),),
                  type: MaterialType.transparency,
                  windowChange: (value){
                    setState(() {
                      spProShowTimerDown=value;
                    });
                  },
                  window:Container(width: MediaQuery.of(context).size.width,color: Colors.black.withOpacity(0.7),child:Column(
                    children: <Widget>[
                      Container(
                        color: Colors.white,
                        child: ListView(
                            padding: EdgeInsets.zero,
                            shrinkWrap: true,
                            physics: const NeverScrollableScrollPhysics(),
                            children: spProTimeKeys[spProBoardTitles.indexOf(spProBoardKey)].map((item) => GestureDetector(
                              child:GestureDetector(
                                behavior: HitTestBehavior.opaque,
                                child: Container(
                                  decoration: BoxDecoration(
                                      color: Colors.white,
                                      border:Border(bottom: BorderSide(width: 0.4,color: Colors.grey[300]!))
                                  ),
                                  padding: EdgeInsets.symmetric(horizontal: width(15),vertical: width(8)),
                                  width: MediaQuery.of(context).size.width,
                                  child: Row(
                                    children: <Widget>[
                                      Text(item,style: TextStyle(color: spProTimeKey==item? Color(0xFFE3494B):Color(0xFF888888)),),
                                      Visibility(
                                        child: Expanded(
                                          child: Row(
                                            mainAxisAlignment: MainAxisAlignment.end,
                                            children: <Widget>[
                                              Icon(Icons.check_circle_outline,color: Color(0xFFE3494B),size: width(18),),
                                            ],
                                          ),
                                        ),
                                        visible: spProTimeKey==item ,
                                      )
                                    ],
                                  ),
                                ),
                                onTap: (){
                                  spProExpertList.clear();
                                  setState(() {
                                    spProTimeKey=item;
                                  });
                                  controller?.callRefresh();
                                  Navigator.of(context).pop();
                                },
                              ) ,
                            )).toList()
                        ),
                      ),
                      Expanded(
                        child: GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          child: SizedBox(width:MediaQuery.of(context).size.width ,),
                          onTap: ()=>Navigator.of(context).pop(),
                        ),
                      )
                    ],
                  ),),
                ),
              ],
            ),
          ),
          Expanded(
            child: Container(
              decoration: BoxDecoration(
                  color: Colors.white,
                  boxShadow:[
                    BoxShadow(
                      offset: Offset(2,5),
                      color: Color(0x0C000000),
                      blurRadius:width(6,),),
                    BoxShadow(
                      offset: Offset(-5,1),
                      color: Color(0x0C000000),
                      blurRadius:width(6,),
                    )
                  ],
                  borderRadius: BorderRadius.circular(width(7))
              ),
              // margin: EdgeInsets.only(left: width(10),right: width(10),top: width(10)),
              margin: EdgeInsets.only(right: width(3),left: width(3)),
              child: EasyRefresh.custom(
                controller: controller,
                onRefresh: onRefresh,
                firstRefresh: true,
                header: SPClassBallHeader(
                    textColor: Color(0xFF666666)
                ),
                footer: SPClassBallFooter(
                    textColor: Color(0xFF666666)
                ),
                emptyWidget: spProExpertList.length==0? SPClassNoDataView():null,
                slivers: <Widget>[
                  SliverToBoxAdapter(
                    child: Container(
                      height: height(30),
                      color: Color(0xFFF1F1F1),
                      child: Row(
                        children: <Widget>[
                          Container(
                            width: width(50),
                            alignment: Alignment.center,
                            child:Text("排行",style: TextStyle(fontSize: sp(12),color: Color(0xFF666666),fontWeight: FontWeight.bold),),
                          ),
                          Container(
                            padding: EdgeInsets.only(left: width(5)),
                            width: width(110),
                            alignment: Alignment.centerLeft,
                            child:Text("专家",style: TextStyle(fontSize: sp(12),color: Color(0xFF666666),fontWeight: FontWeight.bold),maxLines: 1,),
                          ),
                          Expanded(
                            child:Container(
                              alignment: Alignment.center,
                              child: Text(spProBoardTopTitles[spProBoardTitles.indexOf(spProBoardKey)],style: TextStyle(fontSize: sp(11.5),color: Color(0xFF666666),fontWeight: FontWeight.bold),maxLines: 1,),
                            ),
                          ),
                          SizedBox(width: width(30),),
                          spProOrderKeys[spProBoardTitles.indexOf(spProBoardKey)]=="max_red_num"? SizedBox():  Expanded(
                            child:Container(
                              alignment: Alignment.center,
                              child: Text(spProOrderKeys[spProBoardTitles.indexOf(spProBoardKey)]=="popularity"? "查看/付费人数":"成绩",style: TextStyle(fontSize: sp(12),color: Color(0xFF666666),fontWeight: FontWeight.bold),maxLines: 1,),
                            ),
                          ),
                          SizedBox(width: width(54),),
                        ],
                      ),
                    ),
                  ),
                  SliverList(
                    delegate: SliverChildBuilderDelegate(
                          (context, index) {
                        var item=spProExpertList[index];
                        return GestureDetector(
                          child: Container(
                            decoration: BoxDecoration(
                                color: Colors.white,
                                border: Border(bottom: BorderSide(width: 0.4,color: Colors.grey[300]!))
                            ),
                            height: height(44),
                            child: Row(
                              children: <Widget>[
                                Container(
                                  width: width(50),
                                  height: height(48),
                                  alignment: Alignment.center,
                                  child: (index<3&&index>=0) ?
                                  buildMedal(index+1):Text((index+1).toString(),style: TextStyle(fontSize: sp(13),color: Color(0xFF999999)),),
                                ),
                                Container(
                                  width: width(110),
                                  child: Row(
                                    children: <Widget>[
                                      Container(
                                        decoration: BoxDecoration(
                                            border: Border.all(width: 1,color: Colors.grey[200]!),
                                            borderRadius: BorderRadius.circular(width(18))),
                                        child:  ClipRRect(
                                          borderRadius: BorderRadius.circular(width(18)),
                                          child:( item.spProAvatarUrl==null||item.spProAvatarUrl!.isEmpty)? Image.asset(
                                            SPClassImageUtil.spFunGetImagePath("ic_default_avater"),
                                            width: width(36),
                                            height: width(36),
                                          ):Image.network(
                                            item.spProAvatarUrl!,
                                            width: width(36),
                                            height: width(36),
                                            fit: BoxFit.fill,
                                          ),
                                        ),
                                      ),
                                      SizedBox(width: 2,),
                                      Expanded(
                                        child:Text(item.spProNickName??'',style: TextStyle(fontSize: sp(13),color: Color(0xFF333333)),maxLines: 1,overflow: TextOverflow.ellipsis,),
                                      ),
                                    ],
                                  ),

                                ),
                                Expanded(
                                  child: Container(
                                    alignment: Alignment.center,
                                    child: Text(getboardTitleValue(item),style: TextStyle(fontSize: sp(13),color: Color(0xFFE3494B)),maxLines: 1,),
                                  ),
                                ),
                                spProOrderKeys[spProBoardTitles.indexOf(spProBoardKey)]=="max_red_num"? SizedBox(width: width(30),): Expanded(
                                  flex: 2,
                                  child: Container(
                                    alignment: Alignment.center,
                                    child: Text(spProOrderKeys[spProBoardTitles.indexOf(spProBoardKey)]=="popularity"?  "${item.spProSchemeViewNum}/${item.spProSchemeBuyNum}":"${item.spProCorrectSchemeNum}"+
                                        "红"+
                                        "${item.spProDrawSchemeNum}"+
                                        "走"+
                                        "${item.spProWrongSchemeNum}"+
                                        "黑",style: TextStyle(fontSize: sp(13),color: Color(0xFFE3494B)),maxLines: 1,),
                                  ),
                                ),

                                spProTimeKey=='全周期'&&(spProOrderKeys[spProBoardTitles.indexOf(spProBoardKey)]=="correct_rate"||spProOrderKeys[spProBoardTitles.indexOf(spProBoardKey)]=="profit_sum")?SizedBox():Container(
                                  alignment: Alignment.centerLeft,
                                  width: width(54),
                                  child: GestureDetector(
                                      child: Container(

                                        decoration: BoxDecoration(
                                          borderRadius: BorderRadius.circular(width(3)),
                                          gradient: LinearGradient(
                                              colors:item.spProIsFollowing!?  [Color(0xFFC6C6C6),Color(0xFFC6C6C6)]:[Color(0xFFF2150C),Color(0xFFF24B0C)]
                                          ),
                                          boxShadow:item.spProIsFollowing!? null:[
                                            BoxShadow(
                                              offset: Offset(3,3),
                                              color: Color(0x4DF23B0C),
                                              blurRadius:width(5,),),
                                          ],
                                        ),
                                        width: width(44),
                                        height: height(22),
                                        child: Row(
                                          mainAxisAlignment: MainAxisAlignment.center,
                                          crossAxisAlignment: CrossAxisAlignment.center,
                                          children: <Widget>[
                                            Text( item.spProIsFollowing!? "已关注":"关注",style: TextStyle(fontSize: sp(11),color: Colors.white),),

                                          ],
                                        ),
                                      ),
                                      onTap: (){
                                        if(spFunIsLogin(context: context)){
                                          SPClassApiManager.spFunGetInstance().spFunFollowExpert(isFollow: !item.spProIsFollowing!,spProExpertUid: item.spProUserId,context: context,spProCallBack: SPClassHttpCallBack<SPClassBaseModelEntity>(
                                              spProOnSuccess: (result){
                                                if(!item.spProIsFollowing!){
                                                  // if(result.data['task']!=null&&result.data['task']['is_show']!='1'){
                                                  //   SPClassTaskToast.spFunToast(context,SPClassTaskEntity.fromJson(result.data['task']));
                                                  // }
                                                  SPClassToastUtils.spFunShowToast(msg: "关注成功");
                                                  item.spProIsFollowing=true;
                                                }else{
                                                  item.spProIsFollowing=false;
                                                }
                                                if(mounted){
                                                  setState(() {});
                                                }
                                              }
                                          ));
                                        }
                                      }
                                  ),
                                ),
                              ],
                            ),
                          ),
                          onTap: (){
                            SPClassApiManager.spFunGetInstance().spFunExpertInfo(queryParameters: {"expert_uid":item.spProUserId},
                                context:context,spProCallBack: SPClassHttpCallBack(
                                    spProOnSuccess: (info){
                                      SPClassNavigatorUtils.spFunPushRoute(context,  SPClassExpertDetailPage(info));
                                    }
                                ));
                          },

                        );
                      },
                      childCount: spProExpertList.length,
                    ),
                  ),
                ],
              ),

            ),
          )
        ],
      ),
    ) ;
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  Future<void>  onRefresh() async{

    page=1;
    var params;
    var spProRankingType=spProTimeKey.toString();
    if(spProBoardKey=="胜率榜"||spProBoardKey=="回报率"){
      if(spProTimeKey=="近期"){
        spProRankingType="近10场";
      }
    }
    if(spProTimeKey=="近期"&&spProBoardKey=="连红榜"){
      params={
        "fetch_type":"current_red_num",
        "page":"1",
        "${SPClassHomePageState.spProHomeMatchTypeKey}":"1"
      };
    }else{
      params= {"order_key":spProOrderKeys[spProBoardTitles.indexOf(spProBoardKey)],"page":"1","ranking_type":spProRankingType,"${SPClassHomePageState.spProHomeMatchTypeKey}":"1","league_name":SPClassHomePageState.spProHomeMatchTypeKey=='is_zq_expert'?selectzqLeague:selectlqLeague};
    }



    await Future.delayed(Duration(seconds: 1));
    await  SPClassApiManager.spFunGetInstance().spFunExpertList(queryParameters: params,spProCallBack: SPClassHttpCallBack<SPClassExpertListEntity>(
        spProOnSuccess: (list){
          controller?.finishLoad(success: true);
          controller?.resetRefreshState();

          if(mounted){
            setState(() {
              spProExpertList=list.spProExpertList!;
            });
          }
        },
        onError: (value){
          controller?.finishLoad(success: false);
        }
    ));
  }

  Future<void>  onLoading() async{
    var params;
    params= {"order_key":spProOrderKeys[spProBoardTitles.indexOf(spProBoardKey)],"page":(page+1).toString(),"ranking_type":spProTimeKey,"${SPClassHomePageState.spProHomeMatchTypeKey}":"1","league_name":SPClassHomePageState.spProHomeMatchTypeKey=='is_zq_expert'?selectzqLeague:selectlqLeague};

    await SPClassApiManager.spFunGetInstance().spFunExpertList(queryParameters:params,spProCallBack: SPClassHttpCallBack<SPClassExpertListEntity>(
        spProOnSuccess: (list){
          if(list.spProExpertList?.length==0){
            controller?.finishLoad(noMore: true);
          }else{
            controller?.finishLoad(success: true);
            page++;
          }

          if(mounted){
            setState(() {
              spProExpertList.addAll(list.spProExpertList!);
            });
          }
        },
        onError: (value){
          controller?.finishLoad(success: false);
        }
    ));
  }

  buildMedal(int ranking) {
    return Image.asset(
      SPClassImageUtil.spFunGetImagePath(ranking==1? "ic_leaderbord_one":(ranking==2? "ic_leaderbord_two":"ic_leaderbord_three")),
      width: width(25),
    );

  }

  void initList(SPClassExpertListEntity list) {
    if(list.spProExpertList!=null&&list.spProExpertList!.isNotEmpty){
      list.spProExpertList?.forEach((item) {
        item.spProCorrectRate=(double.parse(item.spProLast10CorrectNum!)/item.spProLast10Result!.length).toStringAsFixed(2);
        item.spProCorrectSchemeNum=item.spProLast10Result?.replaceAll("0", "").replaceAll("2", "").length.toString();
        item.spProDrawSchemeNum=item.spProLast10Result?.replaceAll("0", "").replaceAll("1", "").length.toString();
        item.spProWrongSchemeNum=item.spProLast10Result?.replaceAll("1", "").replaceAll("2", "").length.toString();
      });

    }
  }

  String getboardTitleValue(SPClassExpertListExpertList item) {

    if(spProOrderKeys[spProBoardTitles.indexOf(spProBoardKey)]=="correct_rate"){

      try{
        return "${(double.tryParse(item.spProCorrectRate??'0')! *100).toStringAsFixed(0)}%";
      }catch(e){
        return "";
      }
    }

    if(spProOrderKeys[spProBoardTitles.indexOf(spProBoardKey)]=="max_red_num"){
      if(spProTimeKey=="近期"){
        return item.spProCurrentRedNum!;
      }
      return item.spProMaxRedNum!;
    }

    if(spProOrderKeys[spProBoardTitles.indexOf(spProBoardKey)]=="profit_sum"){

      try{
        return "${(double.tryParse(item.spProProfitSum??'0')!*100).toStringAsFixed(0)}%";
      }catch(e){
        return "";
      }
    }

    return "";
  }
}